﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace MCSharpLib.MDataBase
{
    class Queryer
    {
        public abstract object GetSingle(string sql, string conn);
        public abstract DataSet Query(string sql, string conn);
    }
    class MySQLQueryer:Queryer{
        public object GetSingle(string sql, string conn)
        {
            return DbHelperMySQL.GetSingle(sql, conn);
        }
        public DataSet Query(string sql,string conn)
        {
            return DbHelperMySQL.Query(sql, conn);
        }
    }
    class SQLQueryer : Queryer
    {
        public object GetSingle(string sql, string conn)
        {
            return DbHelperSQL.GetSingle(sql, conn);
        }
        public DataSet Query(string sql, string conn)
        {
            return DbHelperSQL.Query(sql, conn);
        }
    }
    public class DataPaging
    {
        private int i_total_page = 0;
        private string str_table, str_field, str_condition;
        private DataTable dt_table = new DataTable();
        private int i_total_rows = 0;
        private Queryer queryer;
        public int CurrentPage { get; set; }
        public int TotalPage
        {
            get
            {
                return i_total_page;
            }
        }
        public int PageSize { get; set; }
        public string ConnectionString { get; set; }
        public bool UseSQL { get; set; }
        public DataTable CurrentData
        {
            get
            {
                return dt_table;
            }
        }
        public int TotalRows
        {
            get
            {
                return i_total_rows;
            }
        }
        public DataTable BindData(string str_field, string str_table, string str_condition, int i_page_size, int i_current_page = 1)
        {
            try
            {
                this.str_table = str_table;
                this.str_field = str_field;
                this.str_condition = str_condition;
                PageSize = i_page_size;
                CurrentPage = i_current_page;
                if (UseSQL)
                    queryer = new SQLQueryer();
                else
                    queryer = new MySQLQueryer();
                i_total_rows = (int)queryer.GetSingle("select count(1) from " + str_table + " where " + str_condition, ConnectionString);
                i_total_page = (int)Math.Ceiling((double)(i_total_rows) / (double)PageSize);
                DataTable dt_data = queryer.Query("select top " + PageSize.ToString() + " id," + str_field + " from " + str_table + " where id not in (select top (" + PageSize.ToString() + "*(" + CurrentPage.ToString() + "-1)) id from " + str_table + " where " + str_condition + " order by id) " + "and " + str_condition + " order by id", ConnectionString).Tables[0];
                dt_data.Columns.Remove("id");
                dt_table = dt_data;
                return dt_table;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public DataTable NextPage()
        {
            if (CurrentPage + 1 <= TotalPage)
                return BindData(str_field, str_table, str_condition, PageSize, CurrentPage + 1);
            return null;
        }
        public DataTable PreviousPage()
        {
            if (!(CurrentPage - 1 <= 0))
            {
                return BindData(str_field, str_table, str_condition, PageSize, CurrentPage - 1);
            }
            return null;
        }
        public DataTable Goto(int i_page)
        {
            if (!(i_page <= 0))
            {
                if (i_page > i_total_page)
                    return Goto(i_total_page);
                return BindData(str_field, str_table, str_condition, PageSize, i_page);
            }
            return null;
        }
    }
}
